package com.css.fxfzypg.modules.message.repository.impl;

import com.css.fxfzypg.modules.message.repository.MessageRepository;
import com.css.fxfzypg.modules.message.repository.entity.MessageEntity;
import com.css.fxfzypg.modules.message.repository.entity.MessagePageList;
import com.css.fxfzypg.web.PlatformSessionContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class MessageRepositoryImpl implements MessageRepository {
    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Override
    public boolean addMessage(MessageEntity messageEntity) {
        String sql = "INSERT INTO public.ypg_message (message_id, message_system, message_generator, message_receiver, message_content, message_remarks) " +
                "VALUES (?, ?, ?, ?, ?, ?);";
        return jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, messageEntity.getMessageId());
                preparedStatement.setString(2, messageEntity.getMessageSystem());
                preparedStatement.setString(3, messageEntity.getMessageGenerator());
                preparedStatement.setString(4, messageEntity.getMessageReceiver());
                preparedStatement.setString(5, messageEntity.getMessageContent());
                preparedStatement.setString(6, messageEntity.getMessageRemarks());
                preparedStatement.setString(7, messageEntity.getMessageStatus());
                return preparedStatement;
            }
        }) > 0;
    }

    @Override
    public MessagePageList queryMessage(String userId, int currentPage, int pageSize) {
        MessagePageList pageList = new MessagePageList(currentPage,pageSize);
        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("SELECT * FROM ypg_message where 1 =1 and message_status = '0' ");
        if (StringUtils.isNotBlank(userId)){
            sql.append("and message_receiver = ? ");
            sql.append("order by message_id");
            params.add(userId );
        }
        List<MessageEntity> list = jdbcTemplate.query(sql.toString(),new BeanPropertyRowMapper<>(MessageEntity.class),userId);

        pageList.setAllNumber(list.size());
        if(pageList.getCurrentPage()==pageList.getPageNumber()){
            pageList.setMessageList(jdbcTemplate.query(sql.toString() + " limit ? OFFSET ?",new BeanPropertyRowMapper<>(MessageEntity.class),userId,pageSize,(currentPage-1)*pageSize));
        }else{
            pageList.setMessageList(jdbcTemplate.query(sql.toString() + " limit ? OFFSET ?",new BeanPropertyRowMapper<>(MessageEntity.class),userId,pageSize,(currentPage-1)*pageSize));
        }
        return pageList;
    }

    @Override
    public MessageEntity getMessageById(String messageId) {
        StringBuffer sql = new StringBuffer();
        MessageEntity messageEntity = new MessageEntity();
        sql.append("SELECT * FROM ypg_message where 1 =1 ");
        if (StringUtils.isNotBlank(messageId)){
            sql.append("and message_id = ? ");
        }
        messageEntity = jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(MessageEntity.class),messageId);
        return messageEntity;
    }

    @Override
    public boolean readMessage(MessageEntity messageEntity) {
        messageEntity.setMessageStatus("1");
        messageEntity.setMessageUpdateUser(PlatformSessionContext.getUserID());
        messageEntity.setMessageUpdatetime(new Date());
        String sql = "update ypg_message set message_status = ? where message_id = ?";
        Object args[] = {messageEntity.getMessageStatus(),messageEntity.getMessageId()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return true;
        }
        return false;
    }
}
